﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderViewModel : BaseNotifyPropertyChangedUI
    {
        public static FolderOperations _FolderOperations;

        public static DispatcherTimer _timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 1, 0), IsEnabled = true };
        public static ThreadSafeQueue<FolderViewModel> SubFoldersToRead = new ThreadSafeQueue<FolderViewModel>();
        public static string _selectedSpecialFolder = string.Empty;
        
        static FolderViewModel()
        {
            _FolderOperations = new FolderOperations();
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }

        static void _timer_Tick(object sender, EventArgs e)
        {
            _timer.Stop();            

            if (SubFoldersToRead.Count > 0)
            {
                List<FolderViewModel> items = new List<FolderViewModel>();
                int retrieved = SubFoldersToRead.DequeueMultiple(items, 5);
                for (int n = 0; n < retrieved; n++)
                {
                    var item = items[n];
                    if (item._subFoldersForUIBinding == null || item._subFoldersForUIBinding.Count == 0)
                    {
                        item._subFoldersForUIBinding = null;                        
                        item.ReadFoldersOnDemand(Int32.MaxValue, ref item._subFoldersForUIBinding, ref item._subFolderList);
                        item.OnPropertyChanged("SubFolders");
                        item.OnPropertyChanged("SubFoldersRequest");
                    }
                }
            }
            _timer.Start();
        }
    }
}
